<script>
selectedCategory = "0";

if (!smaragdAdmin) var smaragdAdmin = {};

smaragdAdmin.editLockEnabled = false;
smaragdAdmin.addCategoryEditorEnabled = false;
if(!smaragdAdmin.categories) smaragdAdmin.categories = new Array();

smaragdAdmin.init = function() {
	if(smaragdAdmin.editLockEnabled)
	{
		//$(".editlock").addClass("editlock-enabled");
		if(smaragdAdmin.addCategoryEditorEnabled)
		{
			$(".category-arrows, .remove-icon, .edit-icon, #addCategoryButton, .category-new").hide();
			$(".category-new").show();
		} else {
			$(".category-arrows, .remove-icon, .edit-icon, #addCategoryButton, .category-new").show();
			$(".category-new").hide();
		}
		$(".editlock").removeClass("editlock-disabled")
	} else {
		$(".category-arrows, .remove-icon, .edit-icon, #addCategoryButton, .category-new").hide();
		$(".editlock").addClass("editlock-disabled");
		$(".category-new").hide();
	}
};

smaragdAdmin.editLockClick = function() {

	console.log("edit lock clciked");
	smaragdAdmin.editLockEnabled = !smaragdAdmin.editLockEnabled;
	
	if(smaragdAdmin.editLockEnabled)
	{
		if(smaragdAdmin.addCategoryEditorEnabled) {
			$(".category-arrows, .remove-icon, .edit-icon").hide();
			$(".category-new, #addCategoryButton").show();
		} else {
			$(".category-arrows, .remove-icon, .edit-icon, #addCategoryButton").show();
			$(".category-new").hide();
		}
		$(".editlock").removeClass("editlock-disabled");
	} else {
		$(".category-arrows, .remove-icon, .edit-icon, #addCategoryButton, .category-new").hide();
		$(".editlock").addClass("editlock-disabled");
	}

};

smaragdAdmin.addCategoryClick = function() {
	if(smaragdAdmin.addCategoryEditorEnabled) {
		$(".category-arrows, .remove-icon, .edit-icon").show();
		$(".category-new").hide();
		$("#addCategoryButton").removeClass("btn-success");
	} else {
		$(".category-arrows, .remove-icon, .edit-icon").hide();
		$(".category-new").show();
		$("#addCategoryButton").addClass("btn-success");
	}
	smaragdAdmin.addCategoryEditorEnabled = !smaragdAdmin.addCategoryEditorEnabled;
};

smaragdAdmin.selectCategory = function(event) {
	var id = $(event.delegateTarget).data("id");
	var name = $(event.delegateTarget).data("name");

	$("#admin-products-edit").hide();
	
	selectedCategory = id;
	smaragdAdmin.selectCategoryAjax(id, name);
};

smaragdAdmin.removeCategory = function(event) {
	var id = $(event.delegateTarget).data("id");

  	options = {
  		title : "Kategória törlése",
  		text : "Bizotsan törli a kategóriát",
  		ok : "Törlés",
  		id : id,
  		callBack : function() {smaragdAdmin.removeCategoryAjax(id);}
    };
  	
  	smaragdAdmin.showRemoveConfiramtionModal(options);
};

smaragdAdmin.editCategory = function(event) {
	var id = $(event.delegateTarget).data("id");
	var name = $(event.delegateTarget).data("name");

	var inputField = '<div class="input-group confirmationModal-input">' +
 						'<input id="confirmValue" type="text" class="form-control" value="' + name + '">'+
  					 '</div>';
  					 
  	options = {
  		title : "Kategória átnevezése",
  		text : inputField,
  		ok : "Módosítás",
  		id : id,
  		callBack : function() {smaragdAdmin.editCategoryAjax(id)}
    };

  	smaragdAdmin.showRemoveConfiramtionModal(options);
};

smaragdAdmin.addCategory = function(event) {
	var id = $(event.delegateTarget).data("id");
	//var name = $(event.delegateTarget).data("name");

	var inputField = '<div class="input-group confirmationModal-input">' +
 						'<input id="confirmValue" type="text" class="form-control" value="">'+
  					 '</div>';
  					 
  	options = {
  		title : "Új kategóra felvétele",
  		text : inputField,
  		ok : "Hozzáad",
  		id : id,
  		callBack : function() {smaragdAdmin.addCategoryAjax(id)}
    };

  	smaragdAdmin.showRemoveConfiramtionModal(options);
};

smaragdAdmin.moveDown = function(event) {
	var id = $(event.delegateTarget).data("id");
	smaragdAdmin.moveDownAjax(id);
};

smaragdAdmin.moveUp = function(event) {
	var id = $(event.delegateTarget).data("id");
	smaragdAdmin.moveUpAjax(id);
};

smaragdAdmin.showRemoveConfiramtionModal = function(event) {
	$('#confirmationModal').modal('show');

	$('#confirmationModal-title').html(options.title);
	$('#confirmationModal-pos').html(options.ok);
	$('#confirmationModal-text').html(options.text);

	$('#confirmationModal-pos').unbind( "click" );
	$('#confirmationModal-pos').click(options.callBack);
};

smaragdAdmin.attachEventHandlers = function() {
	// REMOVE
  	$('.remove-icon').unbind( "click");
	$('.remove-icon').bind( "click", function(event){smaragdAdmin.removeCategory(event);});
			
	// EDIT
	$('.edit-icon').unbind( "click");
	$('.edit-icon').bind( "click", function(event){smaragdAdmin.editCategory(event);});

	// ADD		
	$('.category-new').unbind( "click");
	$('.category-new').bind( "click", function(event){smaragdAdmin.addCategory(event);});

	// CATEGORY SELECTION
	$('.category-item').unbind( "click");
	$('.category-item').bind( "click", function(event){smaragdAdmin.selectCategory(event);});

	// MOVE UP
	$('.up-arrow').unbind( "click");
	$('.up-arrow').bind( "click", function(event){smaragdAdmin.moveUp(event);});

	// MOVE DOWN
	$('.down-arrow').unbind( "click");
	$('.down-arrow').bind( "click", function(event){smaragdAdmin.moveDown(event);});

	// EDIT LOCK CLICK
	$('.editlock').unbind( "click");
	$('.editlock').bind( "click", function(){smaragdAdmin.editLockClick();});

	// ADD CATEGORY
	$('#addCategoryButton').unbind( "click");
	$('#addCategoryButton').bind( "click", function(){smaragdAdmin.addCategoryClick();});
};

//AJAX CALLS //
smaragdAdmin.removeCategoryAjax = function(id) {

	$("#selectedCategory").html(name);

	$.ajax({
        type: "POST",
        url: "/smaragd/admin/removecategory", 
        data: {category_id : id},
        dataType: "text",
        cache: false,
        success: function(response) {
  		    $("#categories_wrapper").replaceWith(response);
  	        $(".category-new").hide();
  	        smaragdAdmin.attachEventHandlers();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("failer");
        }
    });
};

smaragdAdmin.editCategoryAjax = function(id) {
	var newName = $("#confirmValue").val();

	if(selectedCategory == id) {
		$("#selectedCategory").html(newName);
	}
	
	$.ajax({
	    type: "POST",
	    url: "/smaragd/admin/editcategory", 
	    data: {category_id : id, name : newName},
	    dataType: "text",
	    cache: false,
	    success: 
	         function(response) {
	    		$("#categories_wrapper").replaceWith(response);
	    		$(".category-new").hide();
				smaragdAdmin.attachEventHandlers();
	         },
	    error: 
	    	function (xhr, ajaxOptions, thrownError) {
	        	 alert("Edit fail: " + thrownError);
	        }
	 }); 
};

smaragdAdmin.addCategoryAjax = function(id) {
	var name = $("#confirmValue").val();
	
	$.ajax({
	    type: "POST",
	    url: "/smaragd/admin/addcategory", 
	    data: {category_id : id, name : name},
	    dataType: "text",
	    cache: false,
	    success: 
	         function(response) {
	    		$("#categories_wrapper").replaceWith(response);
				smaragdAdmin.attachEventHandlers();
				smaragdAdmin.init();
	         },
	    error: 
	    	function (xhr, ajaxOptions, thrownError) {
	        	 alert("Add fail: " + thrownError);
	        }
	 });
};

smaragdAdmin.selectCategoryAjax = function(id, name) {
	console.log("ajax select call sent");

	$.ajax({
	    type: "POST",
	    url: "/smaragd/admin/getproductsbycategory", 
	    data: {category_id : id, name: name},
	    dataType: "text",
	    cache: false,
	    success: 
	         function(products){
		    	$("#admin-products").replaceWith(products);

		    	if(selectedCategory == id) {
		    		$("#selectedCategory").html(name);
		    	}
				//$("#selectedCategory").html();
		    	smaragdAdmin.attachEventHandlers();
	         },
	    error: 
	    	function (xhr, ajaxOptions, thrownError) {
	        	//alert(xhr.status);
	        	alert(xhr.responseText);
	        }
	 });
};

smaragdAdmin.moveDownAjax = function(id) {

	console.log("move down");
	
	$.ajax({
	    type: "POST",
	    url: "/smaragd/admin/movecategorydown", 
	    data: {category_id : id},
	    dataType: "text",
	    cache: false,
	    success: 
	         function(response) {
	    		$("#categories_wrapper").replaceWith(response);
				$(".category-new").hide();
				smaragdAdmin.attachEventHandlers();
	         },
	    error: 
	    	function (xhr, ajaxOptions, thrownError) {
	        	 alert("failer");
	        }
	 });
};

smaragdAdmin.moveUpAjax = function(id) {	
	console.log("move up");
	
	$.ajax({
	    type: "POST",
	    url: "/smaragd/admin/movecategoryup", 
	    data: {category_id : id},
	    dataType: "text",
	    cache: false,
	    success: 
	         function(response) {
	    		$("#categories_wrapper").replaceWith(response);
				$(".category-new").hide();
				smaragdAdmin.attachEventHandlers();
	         },
	    error: 
	    	function (xhr, ajaxOptions, thrownError) {
	        	 alert("failer");
	        }
	 });
};




smaragdAdmin.init();
smaragdAdmin.attachEventHandlers();
</script>